package com.goods.controller.business;

import com.github.pagehelper.PageInfo;
import com.goods.business.service.HealthService;
import com.goods.common.model.business.Health;
import com.goods.common.response.ActiveUser;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.HealthVO;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Hachimen
 * @date ：Created in 2021/9/27 14:01
 * @description：健康打卡
 * @modified By：
 * @version:
 */

@RestController
@RequestMapping("/business/health")
public class HealthWebController {

    @Autowired
    private HealthService healthService;

    //健康打卡 检测是否打卡
    @GetMapping("isReport")
    public ResponseBean isReport(){
        //获取用户ID
        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long id = principal.getUser().getId();
        HealthVO healthVO = healthService.isReport(id);
        return ResponseBean.success(healthVO);
    }

    //健康打卡 打卡
    @PostMapping("report")
    public ResponseBean report(@RequestBody HealthVO healthVo){
        ActiveUser principal = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        Long id = principal.getUser().getId();
        healthVo.setUserId(id);
        healthService.report(healthVo);
        return ResponseBean.success();
    }

    //健康打卡 分页查询
    @GetMapping("history")
    public ResponseBean history(HttpServletRequest request){
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        PageInfo<Health> page = healthService.history(pageNum,pageSize);
        Map<String, Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getList());
        return ResponseBean.success(data);
    }

}
